<?php
	ini_set("include_path", ".:../:./includes:../includes");

	extract($_REQUEST);
	require_once("includes/constants.php");	
	require_once("includes/common.php");
	require_once("includes/template.php"); 
	require_once("includes/database.php");
	head("Bank Holidays"); 
	
?>

<?php 
	$_SESSION[BANK_HOLIDAYS] = getBankHolidays();
	$today = time();
	$year = strftime("%Y", $today);  //todo: make year selectable
	if (isset($selectedEmployee)) { 
		$_SESSION[EMPLOYEE_HOLIDAYS][$selectedEmployee] = getEmployeeHolidays($_SESSION[EMPLOYEES][$selectedEmployee]['e_employee_id']); 

	}

	echo "<table cellspacing='15px' >";
	$year = 2008; //todo: make year selectable
	echo "<tr><td colspan='3' class='titleStyle'> $year </td></tr>";
	
	for ($month=1; $month <= 12; $month++) {
		if ($month == 1 || $month ==4 || $month == 7 || $month == 10) echo "<tr>";
		echo "<td valign='top' bgcolor='#E6E6FA'>";
		for ($day=1; $day <= 31; $day++) {
			$timestamp = mktime(0,0,0,$month,$day,$year);
			$dat = getdate($timestamp);	
			$foundBankHoliday = false;
			if (isset($_SESSION[BANK_HOLIDAYS])) {
				foreach ($_SESSION[BANK_HOLIDAYS] as $bankHoliday) {
					if ($bankHoliday == $timestamp) {
						$foundBankHoliday = true;						 
						break;
					}
				}
			}
			$foundEmployeeHoliday = false;
			if (isset($_SESSION[EMPLOYEE_HOLIDAYS][$selectedEmployee])) {
				foreach ($_SESSION[EMPLOYEE_HOLIDAYS][$selectedEmployee] as $employeeHoliday) {
					if ($employeeHoliday ==$timestamp ) {
						$foundEmployeeHoliday = true;
						break;
					}
				}
			}						
			echo "<input type='hidden' name='" . 
					$timestamp . "' id='" . $timestamp ."'";
			if ($foundEmployeeHoliday)	{
				echo "value='" . $timestamp. "' ";
			}
			else {
				echo "value='' ";
			}	
			echo "  />";			
			if ($day == 1) { 
				echo "<table  ><tr><td colspan=7 align='center'> <b>" . strtoupper($dat['month']) . "</b></td></tr>";
				echo "<tr><td>&nbsp;Sun</td><td>&nbsp;Mon</td><td>&nbsp;Tue</td><td>&nbsp;Wed</td><td>&nbsp;Thu</td><td>&nbsp;Fri</td><td>&nbsp;Sat</td></tr>";
				if ($dat['wday'] > 0) echo "<tr>";
				for ($i=0; $i < $dat['wday']; $i++) {
						echo "<td>&nbsp;&nbsp;</td>";						 
				}
			}
			if ($dat['mon'] != $month) {
				$lastWeekDay =  $dat['wday'] - 1;
				if ($lastWeekDay == -1) $lastWeekDay = 6;
				for ($i=$lastWeekDay; $i < 6; $i++) {
						echo "<td>&nbsp;&nbsp;</td>";						
				}			
				break;
			}
			if ($dat['wday'] == 0) {
				echo "<tr>";
			}
			echo '<td onmouseover="javascript:this.className='
			. "'hoveredEmployeeHoliday';"
			. '"'
			. 'onmouseout="javascript: ';
			if ($foundBankHoliday) {
				echo "this.className='hoveredBankHoliday';" . '"';
			}	
			else {
				
				 echo 'if (document.getElementById(' . "'$timestamp'" .  ").value == '' ) this.className=";
				 if ($today < $timestamp) {	
					echo  "'unselectedCalendarDate';" .  '"';	
				 }
				 else {
				 	echo  "'pastCalendarDate';" .  '"';
				 }
			}	
			
			
			if ($today < $timestamp) {	
				if (!$foundBankHoliday & $dat['wday'] != 6 & $dat['wday'] != 0 ) {
					echo 'onclick="javascript:if (document.getElementById(' .
					"'$timestamp'" .  ").value == '' ) {document.getElementById('" . 
					$timestamp .  "').value = " .  "'$timestamp'" . ";this.className='hoveredEmployeeHoliday'; } else { document.getElementById('"  .
					$timestamp .  "').value =''; this.className='unselectedCalendarDate'; }" .  '"';				
				}
			}
				
			if ($foundBankHoliday) {
				echo "class='hoveredBankHoliday'";
			}
			
			else if ($foundEmployeeHoliday) {
				echo "class='hoveredEmployeeHoliday'";
			}
			else if ($today < $timestamp) {	
				echo "class='unselectedCalendarDate'";
			}
			else {
				echo "class='pastCalendarDate'";
			}
			
			echo  '>';		
	
			echo "$day </td>";
			if ($dat['wday'] == 6) {
				echo "</tr>";
			}			
			
		}
		echo "</table>";
		echo "</td>";
		if ($month == 3 || $month ==6 || $month == 9 || $month == 12) echo "</tr>";
	}
	echo "</table>";

?>
<?php foot("SAVE", "employeeHolidayLogic.php?selectedEmployee=" . $selectedEmployee); ?>